package com.zwh.autoconfigure.eventbus;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.zwh.common.spring.SpringContextUtils;

/**
 * @author monkey
 * @date 2020/11/10 17:29
 **/
@Component
public class MyApplicationListener  implements ApplicationListener<ApplicationPreparedEvent> {
    @Override
    public void onApplicationEvent(ApplicationPreparedEvent event) {
        EventBusCenter eventBusCenter = SpringContextUtils.getBean(EventBusCenter.class);
        // 获取所有带有 @EventBusListener 的 bean，将他们注册为监听者
        Map<String, Object> beansWithAnnotation = SpringContextUtils.getApplicationContext().getBeansWithAnnotation(EventBusListener.class);
        List<Object> listeners = beansWithAnnotation.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
        eventBusCenter.registry(listeners);
    }


//    @Override
//    public void onApplicationEvent(ApplicationEvent event) {
//        if(event!=null){
//
//        }
//        /*EventBusCenter eventBusCenter = SpringContextUtils.getBean(EventBusCenter.class);
//        // 获取所有带有 @EventBusListener 的 bean，将他们注册为监听者
//        Map<String, Object> beansWithAnnotation = SpringContextUtils.getApplicationContext().getBeansWithAnnotation(EventBusListener.class);
//        List<Object> listeners = beansWithAnnotation.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
//        eventBusCenter.registry(listeners);*/
//    }
}
